﻿namespace Arbalest.Web.Controllers
{
    using Arbalest.Web.Helpers;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;


    public class StockController : Controller
    {
        //
        // GET: /Stock/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult SearchResults(string searchTerm, FormCollection collection)
        {
            // http://www.google.com/finance/historical?q=NASDAQ%3AAREX&output=csv
            ViewBag.FormCollection = collection;
            return View();
        }

        //
        // GET: /Portfolio/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Portfolio/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Portfolio/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Portfolio/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Portfolio/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Portfolio/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Portfolio/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Search(string term)
        {
            try
            {
                List<string> suggestedList = new List<string>();
                DataTable dt = null;
                DataSet ds = DataHelper.SqlServer.COREPROD.FetchDataSet(
                    string.Format("SELECT DISTINCT TOP 20 * FROM investment.Stock WHERE Name LIKE '{0}%';",
                    term.Replace("\'", "\'\'")),  CommandType.Text);

                if ((ds != null) && (ds.Tables.Count > 0) && ds.Tables[0].Rows.Count > 0)
                {
                    dt = ds.Tables[0];
                    foreach (DataRow r in dt.Rows)
                        suggestedList.Add(r["Name"].ToString());
                }

                //return Json(suggestedList, JsonRequestBehavior.AllowGet);

                return Json(DataHelper.MakeDictionary(ds.Tables[0]), JsonRequestBehavior.AllowGet);

            }
            catch (Exception ex)
            {
                return View();
            }
        }
    }
}
